package com.example.demo.practice.enterprisemanager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.httpserver.HttpServer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Declan_Wang on 2018/1/5.
 */

public class AuditInfo extends Activity {
    private HttpServer http = new HttpServer();
    private String HTTP = http.getHttp();
    private int id = -1;
    private JSONArray jSONArray;
    private ArrayList<String> info = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audit_manager);
        Intent intent = getIntent();
        id = intent.getIntExtra("id",-1);
        getVideoData();
        Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源
        Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
            public void onClick(View v) {
                Intent intent = new Intent(AuditInfo.this,AuditManager.class);
                intent.putExtra("id",id);
                startActivity(intent);
                finish();
            }
        });

        Button Btn2 = (Button)findViewById(R.id.button2);//获取按钮资源
        Btn2.setOnClickListener(new Button.OnClickListener(){//创建监听
            public void onClick(View v) {
                Intent intent = new Intent(AuditInfo.this,AuditManager.class);
                intent.putExtra("id",id);
                startActivity(intent);
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jSONArray.get(i);
                    int ID = json.getInt("id");
                    if(id == ID){
                        String EnterpriseName = json.getString("EnterpriseName");
                        String FireFightingEquipment = json.getString("FireFightingEquipment");
                        String TimeOf = json.getString("AuditDate");
                        String[] AuditDates = TimeOf.split("T");
                        String AuditDate = AuditDates[0];
                        info.add(EnterpriseName);
                        info.add(AuditDate);
                        info.add(FireFightingEquipment);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            jSONArray=null;

            setInfo();  //给信息赋值函数，用来测试

            super.handleMessage(msg);
        }
    };

    public void setInfo(){
        TextView text1 = findViewById(R.id.text1);
        TextView text2 = findViewById(R.id.text2);
        TextView text3 = findViewById(R.id.text3);

        text1.setText("企业名称：" + info.get(0));
        text2.setText("申请日期：" + info.get(1));
        text3.setText("设备数量：" + info.get(2));
    }

    private void getVideoData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
                    Request request = new Request.Builder()
                            .url(HTTP + "getCheckList")//请求接口。如果需要传参拼接到接口后面。
                            .build();//创建Request 对象

                    Response response = client.newCall(request).execute();//得到Response 对象
                    if (response.isSuccessful()) {
                        Log.e("kwwl", "response.code()==" + response.code());
                        Log.e("kwwl", "response.message()==" + response.message());
                        //response.body().string()只可以调用一次
                        //此时的代码执行在子线程，修改UI的操作请使用handler跳转到UI线程。
                        jSONArray = new JSONArray(response.body().string());
                        handler.sendEmptyMessage(0);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
